<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程名称" prop="procName">
        <el-input
          v-model="queryParams.procName"
          placeholder="请输入流程名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分组" prop="groupId">
        <el-select v-model="queryParams.groupId" placeholder="请选择分组" clearable>
          <el-option v-for="group in groupList" :key="group.groupId" :label="group.groupName" :value="group.groupId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 240px">
          <el-option v-for="dict in bpm_proc_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:proc:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:proc:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:proc:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="procList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程ID" align="center" prop="procId" width="170"/>
      <el-table-column label="流程名称" align="center" prop="procName" />
      <el-table-column label="状态" align="center" prop="status" >
        <template #default="scope">
          <dict-tag :options="bpm_proc_status" :value="scope.row.status" />
        </template>
      </el-table-column>
<!--      <el-table-column label="当前版本id" align="center" prop="lastVersionId" />-->
      <el-table-column label="分组" align="center" prop="groupName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:proc:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:proc:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <CreateProc ref="createProc" @close="createProcClose"/>
  </div>
</template>

<script setup name="Proc">
import {getCurrentInstance, nextTick, ref} from "vue";
const { proxy } = getCurrentInstance();
import { listProc, getProc, delProc} from "@/api/bpm/proc";
import { listGroup } from "@/api/bpm/group";
const { bpm_proc_status } = proxy.useDict("bpm_proc_status");
import CreateProc from "@/views/bpm/components/CreateProc.vue";


const procList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const groupList = ref([]);
const formFields = ref([]);
const permissions = ref([]);
const nodeData = ref(null);
const formFieldData = ref([]);

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  procName: null,
  status: null,
  lastVersionId: null,
  groupId: null,
})


/** 查询流程定义列表 */
function getList() {
  loading.value = true;
  listProc(queryParams.value).then(response => {
    procList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.procId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs.createProc.show()
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _procId = row.procId || ids.value
  proxy.$refs.createProc.show(_procId)
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _procIds = row.procId || ids.value;
  proxy.$modal.confirm('是否确认删除流程定义编号为"' + _procIds + '"的数据项？').then(function() {
    return delProc(_procIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 查询流程分组列表 */
function getGroupList() {
  listGroup(queryParams.value).then(response => {
    groupList.value = response.data;
  });
}

function createProcClose() {
  init()
}

/** 初始化 */
function init() {
  getList()
  getGroupList()
}

init()
</script>


<style lang="less" scoped>

</style>